//
//  BoyeTool.m
//  itboye2015002
//
//  Created by boye_mac1 on 15/8/26.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "BoyeTool.h"
#import "BoyeHttpClient.h"
@implementation BoyeTool

+ (BOOL)validateIDCardNumber:(NSString *)value {
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    int length =0;
    if (!value) {
        return NO;
    }else {
        length = (int)value.length;
        
        if (length !=15 && length !=18) {
            return NO;
        }
    }
    // 省份代码
    NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
    
    NSString *valueStart2 = [value substringToIndex:2];
    BOOL areaFlag =NO;
    for (NSString *areaCode in areasArray) {
        if ([areaCode isEqualToString:valueStart2]) {
            areaFlag =YES;
            break;
        }
    }
    
    if (!areaFlag) {
        return false;
    }
    
    
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    
    int year =0;
    switch (length) {
        case 15:
            year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
            
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                              options:NSMatchingReportProgress
                                                                range:NSMakeRange(0, value.length)];
            
            
            if(numberofMatch >0) {
                return YES;
            }else {
                return NO;
            }
        case 18:
            
            year = [value substringWithRange:NSMakeRange(6,4)].intValue;
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                              options:NSMatchingReportProgress
                                                                range:NSMakeRange(0, value.length)];
            
         
            
            if(numberofMatch >0) {
                int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                int Y = S %11;
                NSString *M =@"F";
                NSString *JYM =@"10X98765432";
                M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                    return YES;// 检测ID的校验位
                }else {
                    return NO;
                }
                
            }else {
                return NO;
            }
        default:
            return NO;
    }
}



//手机号码验证
+(BOOL) isMobileUseable:(NSString *)mobile{
    
    CGFloat  duration = 0.5;
    if ([mobile isStringNull]) {
        [SVProgressHUD showOnlyStatus:@"账号不能为空" withDuration:duration];
        return NO;
    }
    if (![mobile checkMobile]) {
        [SVProgressHUD showOnlyStatus:@"手机号不合法" withDuration:duration];
        return NO;
    }
    return YES;
}

//验证码
+(BOOL) isIdentiCodeUseable:(NSString *)identCode{
    CGFloat  duration = 0.5;

    //验证码
    if ([identCode  isStringNull]) {
        [SVProgressHUD showOnlyStatus:@"验证码不能为空" withDuration:duration];
        
        return NO;
    }
    if (identCode.length !=6) {
        
        [SVProgressHUD showOnlyStatus:@"验证码长度不合法" withDuration:duration];
        return NO;
    }
    return YES;

}

// 密码验证
+(BOOL) isPassWordUseable:(NSString *)passWord{
    
    CGFloat  duration = 0.5;

    //验证码
    if ([passWord  isStringNull]) {
        [SVProgressHUD showOnlyStatus:@"密码不能为空" withDuration:duration];
        
        return NO;
    }

    if (passWord.length <6 || passWord.length >= 18) {
        
        [SVProgressHUD showOnlyStatus:@"密码长度不合法" withDuration:duration];
        return NO;
    }

    return YES;
}

+(BOOL) textIsNotNull:(NSString *) text{
    if (text == nil) {
        return NO;
    }
    if (text.length == 0) {
        return NO;
    }
    return YES;
}

+(NSString*)getEncryptMobile:(NSString * ) mobile{
    if(mobile.length < 11){
        return @"";
    }
    
    return [NSString stringWithFormat:@"%@****%@" ,[mobile substringToIndex:3],[mobile substringFromIndex:7]];
    
}


//获取图片失败时的默认图片
+(UIImage*)getDefuateImage{
    
    return [UIImage imageNamed:[self getDefuateImageString]];

}

//获得失败时的图片名称
+(NSString*)getDefuateImageString{
    
    return @"DefaultShopImage.png";
}
//判断数组是否可用
+(BOOL )checkArrayUseable:(NSArray *)array{
    
    if (array == nil || array.count == 0 || [array class] == [NSNull class]) {
        return NO;
    }
    return YES;
}


@end
